/*
 * test_levent_s2.cpp
 *
 *  Created on: Apr 1, 2014
 *      Author: killerg
 */

#include "net.h"

#include "share/clock.h"

#include <assert.h>
#include <stdlib.h>
#include <event2/util.h>
#include <event2/event.h>
#include <event2/bufferevent.h>
#include <event2/buffer.h>

#include <set>
using namespace std;

static std::set<conn_t*> s_conns;

static void conn_accept_cb(conn_t* conn) {
//	printf("conn_accept_cb\n");
	s_conns.insert(conn);
}

static void conn_disconnect_cb(conn_t* conn) {
//	printf("conn_disconnect_cb\n");
	s_conns.erase(conn);
//	if (s_conns.empty()) {
//		event_base_loopexit(bufferevent_get_base(conn->bev), NULL);
//	}
}

static void conn_data_cb(conn_t* conn) {
//	printf("conn_data_cb\n");

	bufferevent_write_buffer(conn->bev, bufferevent_get_input(conn->bev));
}


bool test_levent_s2() {
	event_base* base = event_base_new();
	assert(base);

	auto listener = start_listen(
			base, "0.0.0.0", 9876,
			conn_accept_cb,
			conn_disconnect_cb,
			conn_data_cb);


    my_clock_t clock;

	clock.init(500);
    while (true) {
    	clock.begin_update();
       	event_base_loop(base, EVLOOP_NONBLOCK);
    	clock.end_update();
    	clock.wait_frame();
    }
    clock.destroy();


	stop_listen(listener);
	event_base_free(base);

	printf("abc\n");

	return true;
}


